package com.hyst.web;

import java.text.ParseException;
import java.util.ArrayList;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hyst.domain.Trylistenmanage;
import com.hyst.repository.CurriculumScheduleMapper;
import com.hyst.repository.TrylistenmanageMapper;
import com.hyst.service.TrylistenmanageService;
import com.sun.org.apache.xpath.internal.operations.Bool;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.CurriculumSchedule;
import com.hyst.domain.CurriculumScheduleView;
import com.hyst.service.CurriculumScheduleService;

/**
 *
 */
@RestController
@RequestMapping("curriculumSchedules")
@CrossOrigin(maxAge = 3600, origins = "*")
public class CurriculumScheduleController {

    @Autowired
    private CurriculumScheduleMapper mapper;

    @Autowired
    private CurriculumScheduleService curriculumScheduleService;

    @Autowired
    private TrylistenmanageMapper trylistenmanageMapper;

    @GetMapping
    public ResultPage<CurriculumSchedule> getProjects(CurriculumSchedule curriculumSchedule) {
        return ResultPage.success(curriculumScheduleService.list(curriculumSchedule));
    }

    @PostMapping
    public ResultBean<CurriculumSchedule> creat(final CurriculumSchedule curriculumSchedule) {
        return ResultBean.success(curriculumScheduleService.insert(curriculumSchedule));
    }

    /**
     * @param id
     * @return ResultBean<CurriculumSchedule>
     */
    @GetMapping("{id}")
    public ResultBean<CurriculumScheduleView> getByPriKey(@PathVariable("id") final Integer id) {
        return ResultBean.success(curriculumScheduleService.getByPriKey(id));
    }

    /**
     * @param curriculumSchedule
     * @return ResultBean<CurriculumSchedule>
     */
    @PatchMapping("{id}")
    public ResultBean<CurriculumSchedule> update(final CurriculumSchedule curriculumSchedule) {
        return ResultBean.success(curriculumScheduleService.update(curriculumSchedule));
    }

    /**
     * @param curriculumSchedule
     * @return ResultBean<CurriculumSchedule>
     */
    //@PutMapping("{id}")
    @PostMapping("{id}")
    public ResultBean<CurriculumSchedule> updatePatch(@PathVariable("id") final Integer id,
        final CurriculumSchedule curriculumSchedule) {
        curriculumSchedule.setId(id);
        return ResultBean.success(curriculumScheduleService.updateSelective(curriculumSchedule));
    }

    /**
     * @param id
     * @return ResultBean<CurriculumSchedule>
     */
    @DeleteMapping("{id}")
    public ResultBean<CurriculumSchedule> delete(@PathVariable("id") final Integer id) {
        return ResultBean.success(curriculumScheduleService.delete(id));
    }

    @GetMapping("countLastMonth")
    public ResultBean<Map<String, String>> countLastMonth(String studentId) {
        return ResultBean.success(curriculumScheduleService.countLastMonth(studentId));
    }

    @GetMapping("getView")
    public List<CurriculumScheduleView> getViewByStudentId(CurriculumSchedule curriculumSchedule) {
        return curriculumScheduleService.getViewByPurchaseCourse(curriculumSchedule);
    }

    @GetMapping("getByTidAndDate")
    public List<CurriculumScheduleView> getByTeacherIdAndDate(CurriculumSchedule curriculumSchedule)
        throws ParseException {
        return curriculumScheduleService.getByTeacherIdAndDate(curriculumSchedule);
    }

    @PostMapping("add")
    public ResultBean<CurriculumSchedule> add(final CurriculumSchedule curriculumSchedule) {
        return ResultBean.success(curriculumScheduleService.add(curriculumSchedule));
    }

    //deleteByPurchaseCourseId
    @PostMapping("deleteByPurchaseCourseId")
    public ResultBean<String> deleteByPurchaseCourseId(String purchaseCourseId) {
        return ResultBean.success(curriculumScheduleService.deleteByPurchaseCourseId(purchaseCourseId));
    }

    @GetMapping("countByState")
    public ResultBean<List<Map<String, String>>> countByStateAndPc(final String pcid) {
        return ResultBean.success(curriculumScheduleService.countByStateAndPc(pcid));
    }

    @GetMapping("picd")
    public ResultBean<Boolean> addClass(@RequestParam("picd") String picd, @RequestParam("hoursClass") Integer hoursClass) throws Exception {
        if (hoursClass == null || hoursClass == 0) {
            return null;
        }
        return ResultBean.success(curriculumScheduleService.addClass(picd, hoursClass));
    }

    @GetMapping("/video/{id}")
    public ResultBean getVideos(@PathVariable("id") Integer id, @RequestParam("type") String type) {
        if (null != type && type.equals("2")) {

            final Trylistenmanage trylistenmanage = trylistenmanageMapper.getReportData(id);

            if (trylistenmanage != null && StringUtils.isNotBlank(trylistenmanage.getVideo())) {

                List<Map> list = Lists.newArrayList();
                String[] split = trylistenmanage.getVideo().split(",");
                for (String sp : split) {

                    Map map = Maps.newHashMap();
                    map.put("url", sp.split("#")[0]);
                    map.put("dateTime", sp.split("#")[1]);
                    list.add(map);
                }
                return ResultBean.success(list);
            }
        } else {
            CurriculumSchedule curriculumSchedule = new CurriculumSchedule();
            curriculumSchedule.setId(id);
            CurriculumScheduleView view = mapper.getView(curriculumSchedule);
            if (view != null && StringUtils.isNotBlank(view.getVideo())) {

                List<Map> list = Lists.newArrayList();
                String[] split = view.getVideo().split(",");
                for (String sp : split) {

                    Map map = Maps.newHashMap();
                    map.put("url", sp.split("#")[0]);
                    map.put("dateTime", sp.split("#")[1]);
                    list.add(map);
                }
                return ResultBean.success(list);
            }
        }

        return ResultBean.success(new ArrayList<Map>());
    }
}
